package com.nccsjz.back.news.web;
import java.util.ArrayList;
import java.util.List;

import com.nccsjz.back.news.service.NewsService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.News;
import com.nccsjz.utils.Page;
@SuppressWarnings("serial")
public class NewsAction extends BaseAction{
    //存放新闻
	private News news;
    //存放id
	private Long newsId;
    //存放url中的参数
	private Long id;
	/*//存放分页信息
    private Pager pager;
    //页码
    private int pageNo=1;
    //每页多少行
    private int pageSize=5;*/
    //判断标识
    private String flag;
    //存放多选中的id
    private String[] checkbox;
    
    //存放分页数据
    private Page page = new Page(40L);
    
	public String[] getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(String[] checkbox) {
		this.checkbox = checkbox;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/*public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}*/
	private NewsService newsService=null;
	public void setId(long id) {
		this.id = id;
	}

	public Long getNewsId() {
		return newsId;
	}

	public void setNewsId(Long newsId) {
		this.newsId = newsId;
	}
	
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}
	//获取所有新闻
    public String list_News() throws Exception{
    	newsService=new  NewsService();
    	//pager=newsService.getNews(pageSize, pageNo);
    	newsService.findNews(page);  
    	if(page.getData().size() == 0){
			if(page.getPageNo() > 1L)
				page.setPageNo(page.getPageNo()-1);
			else if(page.getPageNo() == 1L)
				page.setPageNo(1L);			
		}
    	newsService.findNews(page);   	
    	return SUCCESS;
    }

    //添加新闻 ＊中文乱码问题
    public String add_News() throws Exception{
    	newsService=new NewsService();
    	newsService.addNews(news);    	
    	return "list";
    }
    //删除新闻
    public String delete_News() throws Exception{
    	newsService=new NewsService();
    	newsService.deleteNews(id);
    	return "list";
    }
    //查看和修改视图
    public String view_News()throws Exception{
    	if(0!=id&&null!=flag){
        	newsService=new NewsService();
        	news=newsService.getNews(id);
    		return "edit";
    	}else{
        	newsService=new NewsService();
        	news=newsService.getNews(id);
    		return "view";
    	 }
    }
    
    //修改新闻
    public String edit_News() throws Exception{
    	newsService=new NewsService();
    	newsService.updateNews(news);
    	return "list";
    }
    //批量删除
    public String deleteBatch_News() throws Exception{
    	newsService=new NewsService();    	
    	List<Long> list=new ArrayList<Long>();
    	if(checkbox != null){
    		for(String s:checkbox){
        		list.add(Long.parseLong(s));
        	}
        	newsService.deleteBatchNews(list);
    	}    	    	
    	return "list";
    	
    }
    /**
     * 前台，查看新闻具体内容。
     * @return
     * @throws Exception
     */
    public String viewCon_News() throws Exception{
     	newsService=new NewsService();
    	news=newsService.getNews(id);
//    	viewMore_News
		return "viewCon";
    }
    /**
     * 前台，查看"更多"新闻内容。
     * @return
     * @throws Exception
     */
    public String viewMore_News() throws Exception{
    	newsService=new  NewsService();
    	//pager=newsService.getNews(pageSize, pageNo);
    	newsService.findNews(page); 
    	if(page.getData().size() == 0){
			if(page.getPageNo() > 1L)
				page.setPageNo(page.getPageNo()-1);
			else if(page.getPageNo() == 1L)
				page.setPageNo(1L);			
		}
    	newsService.findNews(page);
		return "viewMore";
    }

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
    
    
}
